package com.pudu.pudusclient;

import com.DataProcess.Robot;
import com.pudu.pdrobot.App;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by fanyp on 2016/9/2.
 */
public class TargetParameterBuild {

    private static TargetParameterBuild single=null;

    private static ArrayList<MapTarget> TargetList;

    public static double START_SPEED_DOWN_LENGTH = 1;//进入3M范围以内开始减速
    public static double START_SPEED_OFF_LENGTH = 0.2;//进入0.5M范围以内开始停止

    private TargetParameterBuild() {};

    public static TargetParameterBuild getInstance() {
        if (single == null) {
            single = new TargetParameterBuild();
        }

        if( TargetList == null) {
            try {
                String mapString = App.getInstance().getApplicationAtlasData()/*MapData.map*/;

                JSONObject mapData = new JSONObject(mapString);
                JSONObject mapJson = mapData.getJSONObject("map");
                JSONArray elements = mapJson.getJSONArray("elements");

                TargetList = new ArrayList<MapTarget>();
                for (int i = 0; i < elements.length(); i++) {
                    JSONObject objectelement = (JSONObject) elements.get(i);
                    String strType = objectelement.getString("type");
                    if (strType.compareTo("target") == 0) {
                        MapTarget obj = new MapTarget();
                        obj.id = objectelement.getInt("id");
                        JSONArray vector = objectelement.getJSONArray("vector");
                        obj.x = (double) vector.get(0);
                        obj.y = (double) vector.get(1);
                        TargetList.add(obj);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return single;
    }

    public double getParameter(int targetID) {

        double fScaleParameter = 1.0;
        if (targetID <= 0) {
            return fScaleParameter;
        }

        if( TargetList == null) {
            return fScaleParameter;
        }

        MapTarget objMyTarget = null;
        for (int i = 0; i < TargetList.size(); i++) {
            MapTarget obj = TargetList.get(i);
            if (obj.id == targetID) {
                objMyTarget = obj;
                break;
            }
        }

        if (objMyTarget == null) {
            return fScaleParameter;
        }


        double dist = Math.sqrt(Math.pow((objMyTarget.x - Robot.position[0]), 2) +
                Math.pow((objMyTarget.y - Robot.position[1]), 2));
        if (dist <= START_SPEED_OFF_LENGTH) {
            fScaleParameter = 0;
        } else if (dist < START_SPEED_DOWN_LENGTH && dist > START_SPEED_OFF_LENGTH) {
            fScaleParameter = (dist - START_SPEED_OFF_LENGTH) /
                    (START_SPEED_DOWN_LENGTH - START_SPEED_OFF_LENGTH);
        } else {
            fScaleParameter = 1.0;
        }

        return fScaleParameter;

        /*double fScaleParameter = 1.0;

        if(mapDecode == null) {
            return 0.0;
        }

        //找到与目标最短距离的点
        //并且判断最短距离是否小于指定大小
        double distMin = START_SPEED_DOWN_LENGTH;
        Point ptTarget = null;
        for(int i = 0 ; i < points.size(); i ++) {
            Point p1 = points.get(i);
            double dist = Math.sqrt(Math.pow((p1.getStartX() - Robot.pos[0]), 2) +
                    Math.pow((p1.getStartY() - Robot.pos[1]), 2));

            if( dist <= distMin ) {
                distMin = dist;
                ptTarget = p1;
            }
        }

        if( ptTarget != null)
        {
            if( distMin <= START_SPEED_OFF_LENGTH) {
                fScaleParameter = 0;
            }else{
                fScaleParameter = ( distMin - START_SPEED_OFF_LENGTH)/
                        (START_SPEED_DOWN_LENGTH-START_SPEED_OFF_LENGTH) ;
            }
        }

        return fScaleParameter;*/
    }

    public static TargetParameterBuild reLoadData() {
        single = null;
        return getInstance();
    }


    public static class MapTarget extends  Object
    {
        public MapTarget(){};

        int id;
        double x;
        double y;
    }
}
